/**
 * Created by fatrbaby on 17/5/12.
 */

new Vue({
    el: '#vote-app',
    data: {
        categories: {},
        items: {},
        votes: {},
        locked: false,
        vote_id: ""
    },
    mounted: function () {
        var that = this;
        that.vote_id = window.__vote_id;
        axios.get('/lua/api/vote/details?vote_id=' + that.vote_id)
            .then(function (response) {
                var data = response.data;
                var vote = data.data;
                if (data.code != 200) {
                    var sucess=layer.alert(data.message, {icon: 2});
                    layer.style(sucess, {
                        color:'#393D49',
                    });
                } else {
                    that.categories = vote.details.categories || {};
                    that.items = vote.details.items;
                    that.votes = vote.votes;
                    that.renderInfo(vote.details);
                }
            })
            .catch(function (err) {
                console.log(err);
                var error=layer.alert(err.messages, {icon: 2});
                layer.style(error, {
                    color:'#393D49',
                });
            });
    },
    methods: {
        vote: function (id) {
            if (window.__signed == "") {
                var download=layer.alert('请下载APP进行投票', {icon: 2});
                layer.style(download, {
                    color:'#393D49',
                });
                return
            }
            var that = this;
            if (that.locked) {
                var waring=layer.alert('您投票的频率太高', {icon: 2});
                layer.style(waring, {
                    color:'#393D49',
                });
            } else {
                var url = '/lua/api/vote/vote?';

                if (window.__uid == "") {
                    window.location.href = url + 'noLogin=true';
                    return
                }

                var query = 'vote_id=' + that.vote_id + '&item_id=' + id + '&uid=' + window.__uid;

                axios.get(url + query)
                    .then(function (response) {
                        var data = response.data;

                        if (data.code == 200) {
                            that.locked = true;
                            that.votes[id] += 1;
                            setTimeout(function () {
                                that.locked = false;
                            }, 2000);

                            layer.msg("投票成功",{icon: 1});

                        } else {
                            var error=layer.alert(data.message, {icon: 2});
                            layer.style(error, {
                                color:'#393D49',
                            });
                        }
                    })
                    .catch(function (err) {
                        console.log(err);
                        var error=layer.alert(err.message, {icon: 2});
                        layer.style(error, {
                            color:'#393D49',
                        });
                    });
            }
        },
        to_details: function (itemId) {
            window.location = '/appvote/unregister/details?vote_id=' + this.vote_id + '&id=' + itemId + '&uid=' + window.__uid;
        },
        renderInfo: function (vote) {
            document.title = vote.title;
            document.querySelector('#vote-title').innerText = vote.title;
            document.querySelector('#vote-rules').innerHTML = vote.rules;
            document.querySelector('#vote-duration').innerText = vote.duration;
        }
    }
});